home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / cal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  8.5 KB  |  296 lines

  1. /* Next available MSG number is     3 */
  2.  
  3. /*****************************************************************************
  4.       CAL.C
  5.       (C) Copyright 1988-1994 by Autodesk, Inc.
  6.  
  7.       This program is copyrighted by Autodesk, Inc. and is  licensed
  8.       to you under the following conditions.  You may not distribute
  9.       or  publish the source code of this program in any form.   You
  10.       may  incorporate this code in object form in derivative  works
  11.       provided  such  derivative  works  are  (i.) are  designed and 
  12.       intended  to  work  solely  with  Autodesk, Inc. products, and 
  13.       (ii.)  contain  Autodesk's  copyright  notice  "(C)  Copyright  
  14.       1988-1993 by Autodesk, Inc."
  15.  
  16.       AUTODESK  PROVIDES THIS PROGRAM "AS IS" AND WITH  ALL  FAULTS.
  17.       AUTODESK  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF  MER-
  18.       CHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK,  INC.
  19.       DOES  NOT  WARRANT THAT THE OPERATION OF THE PROGRAM  WILL  BE
  20.       UNINTERRUPTED OR ERROR FREE.
  21.  
  22.   Description: The main file of the "Geometry Calculator" ADS application.
  23.  
  24.   Notes:
  25.  
  26.   The Geometry Calculator application consists of the following files:
  27.  
  28.   cal.c      ... The main file establishing link with ADS
  29.   callex.c   ... Lexical analyser
  30.   calexpr.c  ... Syntax analyser
  31.   calmngf.c  ... Management of calculator functions
  32.   calstdf.c  ... Standard calculator functions
  33.   calusrf.c  ... User-defined calculator functions
  34.   calerr.c   ... Error handling
  35.   util.c     ... Some utility functions
  36.  
  37.   callex.h   ... Hearer file for callex.c
  38.   calexpr.h  ... Header file for calexpr.c
  39.   calmngf.h  ... Header file for calmngf.c
  40.   calerr.h   ... Header file for calerr.c
  41.   util.h     ... Header file for util.c
  42.   cal.h      ... Includes all the above include files plus some basic
  43.                  C include files.
  44.  
  45. *****************************************************************************/
  46.  
  47.  
  48. /****************************************************************************/
  49. /*  INCLUDES                                                                */
  50. /****************************************************************************/
  51.  
  52. #define MODULE_ID CAL_C_
  53.  
  54. #include  "cal.h"
  55.  
  56. #include "xmf.h"
  57. #include "ads_ix.h"
  58.  
  59. /* Application name */ 
  60. extern char ads_appname[];
  61.  
  62. /****************************************************************************/
  63. /*  IMPORTED FUNCTIONS                                                      */
  64. /****************************************************************************/
  65.  
  66. void cal_register_standard_functions _((void));
  67. void cal_register_user_functions _((void));
  68.  
  69.  
  70. /****************************************************************************/
  71. /*  STATIC VARIABLES AND FUNCTIONS                                          */
  72. /****************************************************************************/
  73.  
  74. static int calculator_function _((void));
  75. static int load_ADS_functions _((void));
  76. static int execute_ADS_function _((void));
  77.  
  78. static struct {char *name; int msgind; int (*func) _((void));} ADS_funcs_table[] =
  79. {
  80.     /*MSG0*/"CAL",    3,  calculator_function,
  81.     /*MSG0*/"C:CAL",  4,  calculator_function,
  82. };
  83.  
  84.  
  85. /****************************************************************************/
  86. /*.doc load_ADS_functions(internal)*/
  87. /*+
  88.     Standard code for loading the ADS functions from 'ADS_funcs_table[]'.
  89. -*/
  90. /****************************************************************************/
  91.  
  92.  
  93. static int
  94. /*FCN*/load_ADS_functions()
  95. {
  96.     int i, ni = ELEMENTS(ADS_funcs_table);
  97.     char *fnom;
  98.  
  99.     for (i = 0; i < ELEMENTS(ADS_funcs_table); i++)
  100.     {
  101.         if (ads_defun(ADS_funcs_table[i].name, i) != RTNORM) {
  102.             return(RSERR);
  103.         }
  104.     if (ads_regfunc(ADS_funcs_table[i].func, i) != RTNORM) {
  105.         return(RSERR);
  106.     }
  107.  
  108.     /* Now define and register the localized names for each function */
  109.         fnom = XMSG("(various)", ADS_funcs_table[i].msgind);
  110.     if (ads_defun(fnom, i + ni) != RTNORM) {
  111.         return(RSERR);
  112.     }
  113.     if (ads_regfunc(ADS_funcs_table[i].func, i + ni) != RTNORM) {
  114.         return(RSERR);
  115.     }
  116.     }
  117.     return(RSRSLT);
  118. } /*load_ADS_functions*/
  119.  
  120.  
  121. /****************************************************************************/
  122. /*.doc execute_ADS_function(internal)*/
  123. /*+
  124.     Standard code for executing an ADS function from 'ADS_funcs_table[]'.
  125. -*/
  126. /****************************************************************************/
  127.  
  128.  
  129. static int
  130. /*FCN*/execute_ADS_function()
  131. {
  132.     int           i;
  133.  
  134.     if (((i = ads_getfuncode()) < 0) ||
  135.         (i >= ELEMENTS(ADS_funcs_table))) {
  136.         return(RSERR);
  137.     }
  138.  
  139.     ads_retnil();
  140.  
  141.     (*ADS_funcs_table[i].func)();
  142.  
  143.     return(RSRSLT);
  144. } /*execute_ADS_function*/
  145.  
  146.  
  147. /****************************************************************************/
  148. /*.doc main(external)*/
  149. /*+
  150.     The main function.
  151. -*/
  152. /****************************************************************************/
  153.  
  154.  
  155. void
  156. /*FCN*/main(argc, argv)
  157.  
  158.   int  argc;
  159.   char *argv[];
  160. {
  161.     int   request;
  162.     short success         = RSRSLT;
  163.     int   load_first_time = TRUE;
  164.     char errmsg[80];
  165.  
  166.     ads_init(argc,argv);
  167.     cal_err = FALSE;
  168.  
  169.     XMF_init(ads_appname, (void(*)())ads_abort);
  170.  
  171.     for (;;) {
  172.  
  173.         if ((request = ads_link(success)) < 0) {
  174.             sprintf(errmsg,
  175.                     XMSG("cal: bad status from ads_link() = %d\n", 1),
  176.                     request);
  177. #ifdef Macintosh
  178.             macalert(errmsg);
  179. #else
  180.             puts(errmsg);
  181.             fflush(stdout);
  182. #endif /* Macintosh */
  183.         XMF_term();
  184.             ads_exit(1);
  185.         }
  186.         success = RSRSLT;
  187.  
  188.         switch (request) {
  189.  
  190.         case RQXLOAD:
  191.             if (load_first_time) {
  192.                 cal_register_standard_functions();
  193.                 cal_register_user_functions();
  194.                 if (cal_err)
  195.         {
  196.             XMF_term();
  197.                     ads_exit(1);
  198.         }
  199.                 load_first_time = FALSE;
  200.             }
  201.             success = load_ADS_functions();
  202.             break;
  203.  
  204.         case RQSUBR:
  205.             success = execute_ADS_function();
  206.             break;
  207.  
  208.         case RQXUNLD:
  209.         XMF_term();
  210.         break;
  211.         case RQSAVE:
  212.         break;
  213.         case RQQUIT:
  214.         case RQEND:
  215.         XMF_term();
  216.             break;
  217.  
  218.         default:
  219.             break;
  220.         } /*switch*/
  221.     } /*forever*/
  222.  
  223. } /*main*/
  224.  
  225.  
  226. /****************************************************************************/
  227. /*.doc calculator_function(internal)*/
  228. /*+
  229.   Function evaluates arithmetic expression string which it receives 
  230.   in 'buff'. If no result buffer is provided, the function prompts
  231.   for a string.
  232. -*/
  233. /****************************************************************************/
  234.  
  235.  
  236. static int
  237. /*FCN*/calculator_function()
  238.  
  239. {
  240.     struct resbuf *buff;
  241.     int             success;
  242.     vector_real_int value;
  243.     char            line[MAX_LINE_LENGTH+1];
  244.  
  245.     buff = ads_getargs();
  246.  
  247.     do {
  248.         cal_err = 0;                  /* Reset the global error flag */
  249.         
  250.         /* Get the input entier from the string resbuf, or, 
  251.            if not present, prompt the user to enter a string */
  252.         
  253.         if (buff != NULL) {
  254.             if ((buff->restype != RTSTR) || (buff->rbnext != NULL)) {
  255.                 error(27, NULL);
  256.                 return FALSE;
  257.             } else {
  258.                 strcpy(line, buff->resval.rstring);
  259.             }
  260.         } else {
  261.             do {
  262.                 ads_initget(1, NULL);
  263.                 success = ads_getstring(TRUE, XMSG(">> Expression: ", 2), line);
  264.                 
  265.                 if (success == RTCAN) {
  266.                     return RSRSLT;
  267.                 } else if (success != RTNORM) {
  268.                     error(27, NULL);
  269.                     return RSERR;
  270.                 }
  271.             } while (line[0] == EOS);
  272.         }
  273.         
  274.         success = cal_evaluate_expression(line, &value);
  275.         
  276.         if (success) {
  277.             switch (value.type) {
  278.             case vector_type:
  279.                 ads_retpoint(value.v);
  280.                 break;
  281.             case real_type:
  282.                 ads_retreal(value.r);
  283.                 break;
  284.             case int_type:
  285.                 ads_retint((int)value.r);
  286.                 break;
  287.             default:
  288.                 break;
  289.             } /*switch*/
  290.         } 
  291.     } while (!success && (buff == NULL));
  292.  
  293.     return RSRSLT;  /* Returns always true */
  294.  
  295. } /*calculator_function*/
  296.